from odoo import fields, models, _
from odoo.exceptions import UserError


class OpenStockLotWizard(models.TransientModel):
    _name = 'open.stock.lot.wizard'
    _description = 'Open Stock Lot Wizard'

    product_id = fields.Many2one('product.product', string='Product')
    stock_lot_id = fields.Many2one('stock.lot', string='Lot')
    s_week_of_year = fields.Char(string='Week Of Year')
    new_week_of_year = fields.Char(string='New Week Of Year')

    def confirm(self):
        if not self.new_week_of_year:
            raise UserError(_('Please enter new week of year.'))
        if self.stock_lot_id:
            domain = [('id', '=', self.stock_lot_id.id)]
        else:
            domain = [('product_id', '=', self.product_id.id), ('s_week_of_year', '=', self.s_week_of_year)]
        lot_ids = self.env['stock.lot'].search(domain)
        lot_ids.update({'s_week_of_year': self.new_week_of_year})
